/*
 * @Author       : NaN 353362604@qq.com
 * @Date         : 2023-01-12 09:35:49
 * @LastEditTime : 2023-06-06 11:21:46
 * @LastEditors  : NaN 353362604@qq.com
 * @FilePath     : \AdminStration\src\Common\Modal\MoreModal\LabTemplate.jsx
 * @Description  :
 */
import { Fragment, useEffect, useState } from 'react'
import { useBoolean, useRequest } from 'ahooks'
import { Http } from '../../Api'
import { Descriptions, Button } from 'antd'
import { LabTestCurve, SeeLabVideoModal } from '../Modal'

// !!! 压力机模块
export const LabStressMachine = ({ data, className }) => {
    const testEvaluationList = { 0: '合格', 1: '不合格', 2: '无效', 3: '作废' }

    const [ModalState, { setTrue: ModalTrue, setFalse: ModalFalse }] = useBoolean(false)
    const [VideoState, { setTrue: VideoTrue, setFalse: VideoFalse }] = useBoolean(false)
    const [ModalData, setModalData] = useState()
    const [Data, setData] = useState()
    const [Length, setLength] = useState()
    const [loadValue, setLoadValue] = useState()
    const [compressive, setCompressive] = useState()
    const [processStress, setProcessStress] = useState()
    const { runAsync: getData } = useRequest(Http, { manual: true })
    useEffect(() => {
        getData({ url: data.api, path: [data.uuid] }).then(res => {
            const { cubeCount, loadValue, compressiveStrength, processStress } = res
            setData(res)
            setLength(cubeCount)
            setLoadValue(loadValue.split(','))
            setCompressive(compressiveStrength.split(','))
            setProcessStress(JSON.parse(processStress))
        })
    }, [data, getData])
    return (
        <div className="LabStressMachine">
            <Descriptions bordered style={{ marginBottom: 8 }}>
                <Descriptions.Item label="试验机名称" span={3}>
                    {Data?.equipmentName}
                </Descriptions.Item>
                <Descriptions.Item label="试件编号">{Data?.cubeCode}</Descriptions.Item>
                <Descriptions.Item label="试验日期">{Data?.testDate}</Descriptions.Item>
                <Descriptions.Item label="试验面积">{Data?.cubeArea}</Descriptions.Item>
                <Descriptions.Item label="设计强度">{Data?.designStrength}</Descriptions.Item>
                <Descriptions.Item label="强度代表值">{Data?.representativeStrength}</Descriptions.Item>
                <Descriptions.Item label="评定结果">{testEvaluationList[Data?.testEvaluation]}</Descriptions.Item>
            </Descriptions>
            <Descriptions bordered>
                <Descriptions.Item label="试验类型" span={3}>
                    {Data?.testType}
                </Descriptions.Item>
                {Length &&
                    [...new Array(Length).keys()].map(key => (
                        <Fragment key={key}>
                            <Descriptions.Item label="荷载力值">{loadValue[key]}</Descriptions.Item>
                            <Descriptions.Item label="抗折/抗压强度"> {compressive[key]} </Descriptions.Item>
                            <Descriptions.Item label="">
                                <Button
                                    type="link"
                                    onClick={() => {
                                        setModalData(processStress[key])
                                        ModalTrue()
                                    }}
                                >
                                    力值曲线
                                </Button>
                                <Button type="link" onClick={() => VideoTrue()}>
                                    视频回放
                                </Button>
                            </Descriptions.Item>
                        </Fragment>
                    ))}
            </Descriptions>
            {ModalState && (
                <LabTestCurve title="数据曲线" Off={ModalFalse} data={ModalData} type="力值/KN" className={className} />
            )}
            {VideoState && <SeeLabVideoModal title="数据曲线" Off={VideoFalse} data={Data} className={className} />}
        </div>
    )
}

const TemplateList = { LabStressMachine }

export const LabTemplate = ({ data, className }) => {
    return (
        <div className="LabTemplate">
            {(() => {
                const Template = TemplateList[data.type]
                return <Template data={data} className={className} />
            })()}
        </div>
    )
}
